package com.china.produce.controller;

import com.alibaba.fastjson.JSONObject;
import com.china.produce.annotation.Eviction;
import com.china.produce.annotation.ResponseResult;
import com.china.produce.mapper.study.AlgoMapper;
import com.china.produce.pojo.Algo;
import com.china.produce.system.util.QueryDslBuildUtil;
import com.china.produce.task.ITaskTestImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.*;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2022/2/15
 */
@RequestMapping("/algo")
@RestController
@Slf4j
@Eviction
public class AlgoController{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private AlgoMapper algoMapper;

    @Autowired
    private ApplicationContext applicationContext;


    private final String newAppId = "1234";

    public class TES implements Callable<String> {
        private final String ab;

        public TES(String ab) {
            this.ab = ab;
        }

        @Override
        public String call() throws Exception {
            System.out.println("ab " + ab);

            return null;
        }
    }

    @GetMapping("/get/{id}/{params}")
    @ResponseResult
    //@Eviction
    public String getAlgoInfo(@PathVariable("id") int id, Model model, @PathVariable("params") String params)
    {
        ExecutorService executor = new ThreadPoolExecutor(10, 10, 120, TimeUnit.SECONDS, new LinkedBlockingQueue<>());

        Future<String> submit = executor.submit(new TES(params));



        System.out.println(model.getAttribute("wx"));

        log.info("id = {}", id);

        Algo info = algoMapper.getInfo(id);

        return String.valueOf(id);
    }

    @PostMapping("/update")
   // @RequiresPermissions(value = {"algo:update"})
    public String update()
    {
        return "algo/update";
    }

    //@RequiresPermissions("sms:edit")
    @PostMapping("/login")
    public String login(@RequestParam("userName") String userName, @RequestParam("password") String password, Model model)
    {

        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

        log.info("userName = {} password = {} token = {}", userName, password, token);

        try{
           // subject.login(token);

            //subject.isPermitted("/algo/update");

            return "login success";
        }catch (UnknownAccountException e) {
            model.addAttribute("msg", "用户名不存在！");
            return "account not exists";
        }catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误！");
            return "password incorrect";
        }
    }

//    @GetMapping("/send")
//    public void send()
//    {
//        System.out.println("thread name 1 :  " + Thread.currentThread().getName());
//
//        AlgoController bean = applicationContext.getBean(AlgoController.class);

      //  bean.testAsync();

  //      System.out.println("finish ");

//        Map<String,String> map = new HashMap<>(5);
//        map.put("title","发送主题");
//        map.put("body","发布了新订单");
//        map.put("order","*******");
//
//        stringRedisTemplate.convertAndSend("test", JSONObject.toJSONString(map));
//    }

//    @Async
//    public void testAsync() {
//
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("thread name 2  : " + Thread.currentThread().getName());
//    }
}
